// pages/me/me.js
const app = getApp()
import { getPatuser, putUserInfo } from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasUserInfo: false,
    list: [],
    info: {},
    actions: [
      {
        name: "男",
        val: 1
      },
      {
        name: "女",
        val: 2
      }
    ],
    show: false,
    show2: false,
    show3: false,
    show4: false,
    age: "",
    address: "",
    realname: ""
  },
  onChange2(event) {
    // event.detail 为当前输入的值
    this.setData({
      age: event.detail
    })
  },
  onClose2: function () {
    this.setData({ show2: false});
  },
  onOk2 () {
    var that = this
    if (that.data.age != "" && /^([1-9][0-9]{0,1}|100)$/.test(that.data.age)) {
      putUserInfo({
        openid: app.globalData.openid, age: that.data.age
       }).then(res => {
        if (res.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 1500
          })
          getPatuser({ openid: app.globalData.openid }).then(res => {
            app.globalData.userInfo = res.data.user_info
            that.setData({
              list: res.data.quesurvey_list,
              info: res.data.user_info
            })
          })
        }
      })
    } else {
      wx.showToast({
        title: '数字不能为空或不合法！',
        icon: 'none',
        duration: 1500
      })
    }
  },
  phoneClick () {
    if (!this.data.hasUserInfo) {
      wx.navigateTo({
        url: '../canIUse/canIUse'
      })
    } else {
      if (this.data.info.mobile == "" || this.data.info.mobile == null) {
        wx.navigateTo({
          url: '../phone/phone'
        })
      }
    }
  },
  onChange3(event) {
    // event.detail 为当前输入的值
    this.data.address = event.detail
  },
  onClose3: function () {
    this.setData({ show3: false});
  },
  onOk3: function () {
    var that = this
    if (that.data.address != "") {
      putUserInfo({
        openid: app.globalData.openid, address: that.data.address
      }).then(res => {
        if (res.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 1500
          })
          getPatuser({ openid: app.globalData.openid }).then(res => {
            app.globalData.userInfo = res.data.user_info
            that.setData({
              list: res.data.quesurvey_list,
              info: res.data.user_info
            })
          })
        }
      })
    } else {
      wx.showToast({
        title: '地址不能为空！',
        icon: 'none',
        duration: 1500
      })
    }
  },
  onChange4(event) {
    // event.detail 为当前输入的值
    this.data.realname = event.detail
  },
  onClose4: function () {
    this.setData({ show4: false });
  },
  onOk4: function () {
    var that = this
    if (that.data.realname != "") {
      putUserInfo({
        openid: app.globalData.openid, realname: that.data.realname
      }).then(res => {
        if (res.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 1500
          })
          getPatuser({ openid: app.globalData.openid }).then(res => {
            app.globalData.userInfo = res.data.user_info
            that.setData({
              list: res.data.quesurvey_list,
              info: res.data.user_info
            })
          })
        }
      })
    } else {
      wx.showToast({
        title: '地址不能为空！',
        icon: 'none',
        duration: 1500
      })
    }
  },
  genderClick: function () {
    if (!this.data.hasUserInfo) {
      wx.navigateTo({
        url: '../canIUse/canIUse'
      })
    } else {
      this.setData({
        show: true
      })
    }
  },
  addressClick: function () {
    if (!this.data.hasUserInfo) {
      wx.navigateTo({
        url: '../canIUse/canIUse'
      })
    } else {
      this.setData({
        show3: true,
        address: ""
      })
    }
  },
  nameClick: function () {
    if (!this.data.hasUserInfo) {
      wx.navigateTo({
        url: '../canIUse/canIUse'
      })
    } else {
      this.setData({
        show4: true,
        address: ""
      })
    }
  },
  ageClick: function () {
    if (!this.data.hasUserInfo) {
      wx.navigateTo({
        url: '../canIUse/canIUse'
      })
    } else {
      this.setData({
        show2: true,
        age: ""
      })
    }
  },
  onClose() {
    this.setData({ show: false });
  },

  onSelect(event) {
    let _this = this
    putUserInfo({ openid: app.globalData.openid, gender: event.detail.val}).then(res=> {
      if (res.code == 200) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 1500
        })
        getPatuser({ openid: app.globalData.openid }).then(res => {
          app.globalData.userInfo = res.data.user_info
          _this.setData({
            list: res.data.quesurvey_list,
            info: res.data.user_info
          })
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    //查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          console.log("用户授权了");
          getPatuser({ openid: app.globalData.openid }).then(res => {
            app.globalData.userInfo = res.data.user_info
            _this.setData({
              list: res.data.quesurvey_list,
              info: res.data.user_info
            })
          })
          this.setData({
            hasUserInfo: true
          })
        } else {
          //用户没有授权
          console.log("用户没有授权");
        }
      }
    });
  },

  getuseInfoMe: function () {
    //查看是否授权
    if (!this.data.hasUserInfo) {
      wx.navigateTo({
        url: '../canIUse/canIUse'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this
    if (app.globalData.userInfo) {
      getPatuser({ openid: app.globalData.openid }).then(res => {
        _this.setData({
          list: res.data.quesurvey_list,
          info: res.data.user_info
        })
      })
      this.setData({
        hasUserInfo: true
      })
    }
  },

  jiluClick (e) {
    let num = e.target.dataset.text
    let typeid = this.data.list[num].type_id
    let id = this.data.list[num].id
    if (typeid == 1) {
      wx.navigateTo({
        url: '../huixian1/huixian1?id=' + id
      })
    } else if (typeid == 2) {
      wx.navigateTo({
        url: '../huixian2/huixian2?id=' + id
      })
    } else if (typeid == 3) {
      wx.navigateTo({
        url: '../huixian3/huixian3?id=' + id
      })
    } else if (typeid == 4) {
      wx.navigateTo({
        url: '../huixian4/huixian4?id=' + id
      })
    } else {
      wx.navigateTo({
        url: '../huixian5/huixian5'
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})